//房屋详情页
import 'package:enjoy_plus_hm/utils/eventBus.dart';
import 'package:enjoy_plus_hm/utils/request.dart';
import 'package:enjoy_plus_hm/utils/toast.dart';
import 'package:flutter/material.dart';

class HouseDetail extends StatefulWidget {
  HouseDetail({super.key, required this.houseId});
  String houseId;

  @override
  State<HouseDetail> createState() => _HouseDetailState();
}

class _HouseDetailState extends State<HouseDetail> {
  //房屋id
  String houseId = '';
  Map houseInfo = {
    'point': '', //小区信息
    'building': '', //楼栋信息
    'id': '', //id
    'name': '', //业主
    'mobile': '', //手机号
    'room': '', //房间号
    'gender': 0, //性别
    'status': 0, //状态
    'idcardBackUrl': '', //身份证背面地址
    'idcardFrontUrl': '', //身份证正面地址
  };
  //加载框
  bool _isloading = false;

  @override
  void initState() {
    super.initState();
    houseId = widget.houseId;
    //获取详情页数据
    getHouseDetail();
  }

  //获取房屋详情
  getHouseDetail() async {
    setState(() {
      _isloading = true;
    });
    try {
      final res = await http.get('/room/$houseId');
      if (res['code'] != 10000) return ToastUtil.showError(res.message);
      //延迟1秒
      Future.delayed(Duration(seconds: 1), () {
        setState(() {
          _isloading = false;
          houseInfo = res['data'];
        });
      });
    } catch (e) {
      ToastUtil.showError('获取失败');
    }
  }

  // 删除按钮二次弹窗
  showConfirmDialog() {
    return showDialog(
      context: context,
      builder: (ctx) {
        return AlertDialog(
          title: Text('温馨提示'),
          content: Text('确定要删除此房屋吗?'),
          actions: [
            //取消按钮
            TextButton(
              child: Text('取消'),
              onPressed: () {
                Navigator.of(context).pop();
              },
            ),
            //确定按钮
            TextButton(
              onPressed: () {
                deleteHouse();
              },
              child: Text('确定'),
            ),
          ],
        );
      },
    );
  }

  //删除房屋
  deleteHouse() async {
    try {
      final res = await http.delete('/room/$houseId');
      if (res['code'] != 10000) return ToastUtil.showError(res.message);
      Navigator.popUntil(context, (route) {
        return route.settings.name == '/house_list';
      });
      event.fire(DeleteHouseEvent());

      ToastUtil.showSuccess('删除房屋成功!!!');
    } catch (e) {
      ToastUtil.showError('删除失败');
    }
  }

  //编辑房屋  -->跳转表单页
  editHouse() {
    Navigator.pushNamed(
      context,
      '/house_form',
      arguments: {'editForm': houseInfo},
    );
  }

  //详情页标签处理  审核中  or  审核失败  or  审核通过
  Widget tagBuilder(int status) {
    List tagList = [
      {},
      {
        "bgColor": const Color.fromARGB(50, 91, 177, 227),
        "textColor": const Color.fromARGB(255, 85, 145, 175),
        "title": "正在审核",
      },
      {
        "bgColor": const Color.fromARGB(255, 91, 243, 91),
        "textColor": const Color.fromRGBO(1, 50, 1, 1),
        "title": "审核成功",
      },
      {
        "bgColor": const Color.fromARGB(255, 247, 129, 133),
        "textColor": const Color.fromARGB(255, 84, 1, 1),
        "title": "审核失败",
      },
    ];

    return Container(
      padding: const EdgeInsets.only(left: 5, right: 5, top: 2, bottom: 2),
      decoration: BoxDecoration(
        color: tagList[status]['bgColor'],
        borderRadius: BorderRadius.circular(5),
      ),
      child: Text(
        '${tagList[status]['title']}',
        style: TextStyle(color: tagList[status]['textColor']),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      // 设置导航条
      appBar: AppBar(
        title: Text('房源详情', style: TextStyle(color: Colors.white)),
        backgroundColor: Colors.pink,
        centerTitle: true,
      ),
      // 设置主体内容
      body: _isloading
          ? Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [CircularProgressIndicator(), Text('加载中...')],
              ),
            )
          : Container(
              padding: EdgeInsets.all(10),
              child: Column(
                children: [
                  Expanded(
                    child: ListView(
                      children: [
                        //第一行文案房屋信息
                        Text(
                          '房屋信息',
                          style: TextStyle(fontSize: 14, color: Colors.grey),
                        ),
                        SizedBox(height: 10),
                        //第二行具体信息
                        Container(
                          padding: EdgeInsets.all(10),
                          width: MediaQuery.of(context).size.width,
                          color: Colors.white,
                          child: Row(
                            children: [
                              Expanded(
                                child: Text(
                                  '${houseInfo['point']}${houseInfo['building']}${houseInfo['room']}室',
                                ),
                              ),
                              // Text('审核中'),
                              tagBuilder(houseInfo['status']),
                            ],
                          ),
                        ),
                        SizedBox(height: 10),
                        //第三行业主信息文案
                        Text(
                          '业主信息',
                          style: TextStyle(fontSize: 14, color: Colors.grey),
                        ),
                        SizedBox(height: 10),

                        //第四行业主信息
                        Container(
                          padding: EdgeInsets.all(10),
                          width: MediaQuery.of(context).size.width,
                          color: Colors.white,
                          child: Column(
                            children: [
                              //房间号行
                              Row(
                                children: [
                                  SizedBox(width: 60, child: Text('房间号：')),
                                  Expanded(child: Text('${houseInfo['room']}')),
                                ],
                              ),
                              SizedBox(height: 10),
                              //业主行
                              Row(
                                children: [
                                  SizedBox(width: 60, child: Text('业主：')),
                                  Expanded(child: Text('${houseInfo['name']}')),
                                ],
                              ),
                              SizedBox(height: 10),
                              // 手机号行
                              Row(
                                children: [
                                  SizedBox(width: 60, child: Text('手机号：')),
                                  Expanded(
                                    child: Text('${houseInfo['mobile']}'),
                                  ),
                                ],
                              ),
                            ],
                          ),
                        ),
                        SizedBox(height: 10),

                        //照片
                        Container(
                          padding: EdgeInsets.all(10),
                          // height: 400,
                          color: Colors.white,
                          child: Column(
                            mainAxisAlignment: MainAxisAlignment.start,
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Text(
                                '本人身份证照片',
                                style: TextStyle(color: Colors.yellow),
                              ),
                              SizedBox(height: 10),
                              Image.network(
                                '${houseInfo['idcardFrontUrl']}',
                                fit: BoxFit.contain,
                              ),
                              SizedBox(height: 20),
                              Image.network(
                                '${houseInfo['idcardBackUrl']}',
                                fit: BoxFit.contain,
                              ),
                            ],
                          ),
                        ),
                      ],
                    ),
                  ),

                  Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      ElevatedButton(
                        onPressed: () {
                          showConfirmDialog();
                          // deleteHouse();
                        },
                        child: Column(
                          children: [
                            SizedBox(height: 8, width: 120),
                            Icon(Icons.delete),
                            Text('删除房屋'),
                            SizedBox(height: 8),
                          ],
                        ),
                      ),
                      // SizedBox(width: 50),
                      ElevatedButton(
                        onPressed: () {
                          editHouse();
                        },
                        child: Column(
                          children: [
                            SizedBox(height: 8, width: 120),
                            Icon(Icons.edit),
                            Text('编辑房屋'),
                            SizedBox(height: 8),
                          ],
                        ),
                      ),
                    ],
                  ),
                ],
              ),
            ),
    );
  }
}
